﻿using System;
using System.Collections.Generic;
using System.Text;
using GlobalHelper;
using IBDM2IBSInterface;
using TimeOperator;

namespace Models
{
    public class TurnModel
    {
        private IList<DepartureModel> _dapartures = new List<DepartureModel>();

        public int Id { get; set; }
        public int TurnNumber { get; set; }
        public int TokenCount { get; set; }
        public IStation Station { get; set; }
        public IStation ToStation { get; set; }
        public DirectionState Direction { get; set; }

        public IList<DepartureModel> Dapartures { get { return _dapartures; } set { _dapartures = value; } }
        public TimeTableModel OwnTimeSheet { get; set; }
        public int Count { get { return Dapartures.Count; } }
        public DepartureModel LastDep { get { if (_dapartures != null) return _dapartures[_dapartures.Count - 1]; return null; } }
        public DepartureModel FirstDep { get { if (_dapartures != null) return _dapartures[0]; return null; } }

        public TurnModel() { }
        protected TurnModel(int number, IStation station,IStation toStation)
        {
            TurnNumber = number;
            Station = station;
            ToStation = toStation;
        }
        //protected TurnModel(int number, IStation station, int tokenCount)
        //{
        //    TurnNumber = number;
        //    Station = station;
        //    TokenCount = tokenCount;
        //}
        public TurnModel(int turnNumber, IStation station, IStation toStation, DirectionState direction)
        {
            this.Direction = direction;
            this.TurnNumber = turnNumber;
            Station = station;
            ToStation = toStation;
        }
        //public TurnModel(int number, IStation station, int tokenCount, DirectionState direction)
        //{
        //    this.Direction = direction;
        //    this.Id = number;
        //}


       
        /// <summary>
        /// 返回周转 第一个 车距
        /// </summary>
        public int FirstHeadway
        {
            get
            {
                if (Dapartures == null || Dapartures.Count <= 1)
                    return 0;

                int result = 0;

                var firstDep = new HourAndMinute(Dapartures[0].Time);
                var secondDep = new HourAndMinute(Dapartures[1].Time);

                result = firstDep.Balance(secondDep);

                return result;
            }
        }
        /// <summary>
        /// 返回周转 最后一个 车距
        /// </summary>
        /// <returns></returns>
        public int LastHeadway
        {
            get
            {
                //该路牌不完整
                if (Dapartures.Count != TokenCount)
                {
                    throw new Exception("周转数据生成不完整！");
                }

                if (Dapartures == null || Dapartures.Count <= 1)
                    return 0;

                int result = 0;

                int position = Dapartures.Count - 1;

                var firstDep = new HourAndMinute(Dapartures[position - 1].Time);
                var secondDep = new HourAndMinute(Dapartures[position].Time);

                result = firstDep.Balance(secondDep);

                return result;
            }
        }


       
        #region Action

        public IList<int> ToList()
        {
            IList<int> result = new List<int>();

            foreach (DepartureModel daparture in _dapartures)
            {
                result.Add(daparture.Time);
            }

            return result;
        }

        #endregion
    }
}
